package com.yanqu.road.server.cmd.activity.washe.jinbiao;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.washe.WasheModule;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = GameProtocol.S_WASHE_JIN_BIAO_PEAK_JOIN_ROOM, desc = "")
public class WasheJinBiaoPeakJoinRoomCmd extends AbstractCommand {

    private int round;
    private long roomId;
    private int cardId;
    private List<Integer> skillIdList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WasheProto.WasheJinBiaoPeakJoinRoomReqMsg reqMsg = WasheProto.WasheJinBiaoPeakJoinRoomReqMsg.parseFrom(packet.getBytes());
        round = reqMsg.getRound();
        cardId = reqMsg.getCardId();
        skillIdList = reqMsg.getSkillIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        WasheConfig config = WasheMgr.getWasheConfig();
        if (config == null) {
            ret = GameErrorCode.E_WASHE_NO_SYSTEM;
            return;
        }
        WasheModule washeModule = player.getModule(WasheModule.class);
        if (washeModule.getWasheUserData() == null) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        // 检查盘面技能
        skillIdList = WasheMgr.removeZeroSkillId(skillIdList);
        ret = WasheMgr.checkChooseSkill(skillIdList);
        if (ret != 0) {
            return;
        }

        ret = player.getModule(WasheModule.class).jinBiaoPeakJoinRoomByCmd(round, cardId, skillIdList);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            WasheProto.WasheJinBiaoPeakJoinRoomRespMsg.Builder builder = WasheProto.WasheJinBiaoPeakJoinRoomRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_WASHE_GET_JIN_BIAO_PEAK_JOIN_ROOM, builder);
        }
    }
}
